package com.sf.fy.uav.applet.order.controller.task;

import com.alibaba.fastjson.JSONObject;
import com.sf.fy.uav.applet.user.service.AboutUserService;
import com.sf.fy.uav.common.awt.RepeatSubmit;
import com.sf.fy.uav.common.request.common.IdRequest;
import com.sf.fy.uav.common.request.order.task.TaskGoodsRequest;
import com.sf.fy.uav.common.request.order.task.TaskRequest;
import com.sf.fy.uav.common.response.IdResponse;
import com.sf.fy.uav.common.response.order.task.GoodsTypeResponse;
import com.sf.fy.uav.common.response.order.task.TaskStatusLogResponse;
import com.sf.fy.uav.common.response.user.OrderDetailResponse;
import com.sf.fy.uav.common.utils.Rs;
import com.sf.fy.uav.order.controller.OrderInfoRest;
import com.sf.fy.uav.order.service.IOrderInfoService;
import com.sf.fy.uav.task.rest.TaskRest;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;

import io.swagger.annotations.Api;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;

/**
 * @author LTQ
 */
@RestController
@RequestMapping("/sys/order/task/")
@Api(tags = "订单controller")
public class TaskController {

    @Autowired
    private TaskRest taskRest;

    @Autowired
    private AboutUserService aboutUserService;

    @Autowired
    private OrderInfoRest orderInfoRest;

    @Autowired
    private IOrderInfoService iOrderInfoService;


    @PostMapping("add-task")
    public Rs<IdResponse> addTask(@RequestBody @Validated TaskRequest taskRequest) {
        return Rs.success(aboutUserService.addTask(taskRequest));
    }

    @PostMapping("cancel-order")
    @RepeatSubmit(interval = 2000, message = "重复提交")
    @Operation(tags = "取消订单", description = "取消订单")
    public Rs<Date> cancelOrder(@RequestBody @Valid IdRequest idRequest) {
        return Rs.success(iOrderInfoService.cancelExpressOrder(idRequest));
    }

    @PostMapping("expire-order")
    public Rs<Date> expireOrder(@RequestBody IdRequest idRequest) {
        aboutUserService.expireOrder(idRequest.getId());
        return Rs.success();
    }

    @GetMapping("get-order-info")
    public Rs<JSONObject> getOrderInfo(@RequestParam @Valid @NotNull String orderCode) {
        return Rs.success(aboutUserService.getOrderInfo(orderCode));
    }

    @GetMapping("get-task-detail")
    public Rs<OrderDetailResponse> getOrderDetail(@RequestParam String id) {
        return Rs.success(aboutUserService.getOrderDetail(id));
    }

    @GetMapping("get-task-status-log")
//    @Cacheable(value = "CITY::TASK_STATUS_LOG", key = "#taskId")
    public Rs<List<TaskStatusLogResponse>> getTaskStatusLog(@RequestParam String taskId) {
        List<TaskStatusLogResponse> taskStatusLogResponses = aboutUserService.getTaskStatusLog(taskId);
        // 根据两个字段进行倒序排序
        taskStatusLogResponses.sort(Comparator.comparing(TaskStatusLogResponse::getCreateTime, Comparator.reverseOrder()).thenComparing(TaskStatusLogResponse::getStatus));

        return Rs.success(taskStatusLogResponses);
    }


    @PostMapping("update-goods")
    public Rs<GoodsTypeResponse> updateGoods(@RequestBody TaskGoodsRequest taskGoodsRequest) {
        aboutUserService.updateGoods(taskGoodsRequest);
        return Rs.success();
    }


}
